/*
MyCart
*/
//var Dtimeout;
//var Dspeed;
var Dtimer=null;
var Dtimer2=null;
var Mdisplay=false;
var hideDiv;
var Container;
var button;
var jqShowObj;
var offset;
var height;
var width;
var btnHeight;
var btnWidth;
function initDMenu(obj,showobj,Dtimeout,Dspeed){
//	Dtimeout=Dtimeout?Dtimeout:300;
//	Dspeed=Dspeed?Dspeed:100;
	hideDiv=$("<div style='z-index:10000;'></div>");
	Container=$("<div id=\"Container\"></div>");
	hideDiv.append(Container);
	button=$(obj);
	jqShowObj=$(showobj);
	offset=button.offset();
	height=jqShowObj.height();
	width=jqShowObj.width();
	btnHeight=button.height();
	btnWidth=button.width();
	$(document.body).prepend(hideDiv);
}
function showDMenu(){
	if (Mdisplay==true){
		return false;
	}else{
		Mdisplay=true;
		Container.css({margin:"0 auto",width:btnWidth+"px",height:btnHeight+"px"});
		hideDiv.css({position:"absolute",top:offset.top+16+"px",left:button.offset().left-35+"px",height:height+"px",width:width+"px"}).show();
		Container.css({border:"1px solid #666666"});
		Container.animate({marginTop:10,height:height+4,width:width+4,opacity:'100'},100,function(){
			jqShowObj.show();
			Container.append(jqShowObj);
			Container.css({border:"0px"});
			jqShowObj.mouseover(function(){
				clearTimeout(Dtimer);clearTimeout(Dtimer2);
			}).mouseout(function(){
				hideDMenu();
			});			
		});
	}
}
function hideDMenu(){
	clearTimeout(Dtimer);
	clearTimeout(Dtimer2);
	Dtimer=setTimeout(function(){
		Container.css({border:"1px solid #666666"});
		$(document.body).prepend(jqShowObj);
		jqShowObj.hide();
		Container.empty();
		Container.animate({width:btnWidth,height:btnHeight,marginTop:'0',opacity:'0'},100,function(){
			Container.hide();
			hideDiv.hide();
			Mdisplay=false;
			});
		}, 100); 
}

/*-----------------------------*/
$(function(){
	initDMenu("#CartSwitch_wrap","#MyCart");
	$("#CartSwitch_wrap").mouseover(function(){
		clearTimeout(Dtimer);
		height=$("#MyCart").height();
		Dtimer=setTimeout("showDMenu()",100); 
	}).bind("mouseleave",function(){
		clearTimeout(Dtimer); 
		if(Mdisplay==true){
			Dtimer2=setTimeout("hideDMenu()",100);
		}
	}); 
	//获取cart数据
	Ajax.call('flow.php?step=cart_list', '', cartListResponse, 'POST', 'JSON');
})

function cartListResponse(result)
{
	var list = result.content;
	var sum = result.total;
	$("#ProductNum").text(sum.real_goods_count);
	$("#TotalNum").text(sum.real_goods_count);
	$("#TotalPrice").text(sum.goods_price);	
	var str = '';
	for (i = 0; i < list.length; i++) 
	{     
		str += '<li id="list_'+list[i].rec_id+'"><dl><dt><a target="_blank" href="goods.php?id='+list[i].goods_id+'"><img alt="'+list[i].goods_name+'" src="'+list[i].goods_thumb+'"/></a></dt> <dd class="p_Name"><a href="goods.php?id='+list[i].goods_id+'">'+list[i].goods_name+'</a></dd><dd class="p_Price"><strong id=price_'+list[i].rec_id+'>'+list[i].goods_price+'</strong>×</strong id=num_'+list[i].rec_id+'>'+list[i].goods_number+'</strong><br/><a href="javascript:void(0)" onclick="quickDeleteCart('+list[i].rec_id+')">删除</a></dd></dl></li>';                   
		//alert(list[i].rec_id);                
	}
	$("#CartContent").append(str);

}

function quickDeleteCart(id)
{	
	Ajax.call('flow.php?step=quick_delete_cart', 'id='+id, quickDeleteCartResponse, 'GET', 'JSON');
}

function quickDeleteCartResponse(result)
{
	if (result.error > 0)
	{
		alert("删除失败，请稍后再试~~");
	}
	else
	{
		//将快捷提示删除，修改总金额，总个数
		$("#list_"+result.content).remove();
		//修改总个数
		var sum = result.total;
		$("#ProductNum").text(sum.real_goods_count);
		$("#TotalNum").text(sum.real_goods_count);
		$("#TotalPrice").text(sum.goods_price);	
		//如果是购物车页面，将相应的项也删除，并修改总金额，总个数
		if ($("#flow_"+result.content))
		{
			$("#flow_"+result.content).remove();
		}
		$("#flow_result").text("购物金额小计 "+sum.goods_price+"元，比市场价 "+sum.market_price+"元 节省了 "+sum.saving+"元 ("+sum.save_rate+") ");	
	}
}